package measurement.motion;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import wiiusej.Wiimote;
import wiiusej.apptest.statistics.AccelerationPaquetList;
import wiiusej.apptest.statistics.PacketManager;
import wiiusej.apptest.statistics.PacketWraped;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
import wiiusej.wiiusejevents.utils.WiimoteBasicListener;
import wiiusej.wiiusejevents.utils.WiimoteListener;
import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;
import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;

public class WiimoteMotionListener implements WiimoteBasicListener{

	//Logger
	protected static Logger fileLogger;
	
	//Concurrent Main Class
	protected static ExecutorService exe;
	
	//Wiimote object representation
	protected Wiimote wiimote;
	
	//Measurement length in packets
	protected Long measurementLength;
	
	
	protected PacketWraped myPackets;
	protected AccelerationPaquetList accPacketList;
	
	protected long randomID;
	
	public WiimoteMotionListener(ExecutorService exe, Wiimote aux, String measurementLength) {
		this.exe = exe;
		this.wiimote = aux;
		
		this.accPacketList = new AccelerationPaquetList(wiimote.getId());
		this.myPackets = new PacketWraped(accPacketList);
		
		this.measurementLength = Long.parseLong(measurementLength);
		
		Random random = new Random(System.currentTimeMillis());
		this.randomID = random.nextLong();
		
		PropertyConfigurator.configure("log4j.properties");
		this.fileLogger = Logger.getLogger("wiimoteMotionListener");
		
		
		//register this listener in the Wiimote's listener list
		registerListeners();
		
		activateMotionSensing();
	}
	
	
	private void activateMotionSensing() {
		wiimote.activateMotionSensing();		
	}
	
	/***********************************************************
	//WIIMOTE LISTENER INTERFACE 
	************************************************************/

	@Override
	public void onButtonsEvent(WiimoteButtonsEvent e) {
		
		
	}

	@Override
	public void onDisconnectionEvent(DisconnectionEvent e) {
		
		
	}

	@Override
	public void onMotionSensingEvent(MotionSensingEvent e, long packetRefnumber, Timestamp packetTime) {
		
		if(packetRefnumber > measurementLength){		
			fileLogger.info("");
			removeListener();
			fileLogger.info("MEASUREMENT FINISHED!!!");			
		}else{		
			getStatistics(e);
		}
				
	}

	@Override
	public void onStatusEvent(StatusEvent e) {
		
		
	}
	

	@Override
	public void registerListeners() {
		//We have to add our listener to the wiimote object
		fileLogger.info("Wiimote Motion Listener - Registering in Wiimote List");
		wiimote.addWiiMoteEventListeners(this);	
		
	}
	
	public void removeListener(){
		fileLogger.info("Wiimote Motion Listener - Registering in Wiimote List");
		wiimote.removeWiiMoteEventListeners(this);
	}
	
	private void getStatistics(MotionSensingEvent arg0){

		SimpleDateFormat tsFormat = new SimpleDateFormat("mm:ss:SS");
		Timestamp packetTime = arg0.getTime();		
		
		fileLogger.info("EventReceived - Wiimote: " + arg0.getWiimoteId() +
				" Packet: " + arg0.getRefNumber() +
				" Time: " + tsFormat.format(arg0.getTime())
				+ " AccVector: " + arg0.getRawAcceleration());
		
		//WARNING: ADD FROMSYSTEM QUEUE TO CALL PACKAGE MANAGER!!
		Future future = 
			exe.submit(new PacketManager(arg0, wiimote.getId(), randomID, myPackets, null, null));	
		
		try {
			myPackets = (PacketWraped) future.get();
			
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		} catch (ExecutionException e1) {
			e1.printStackTrace();
		}		
	}

}
